<?php
//	$id
$CI = &get_instance();
$CI->load->database();
$CI->db->from('tbl_donhang');
$CI->db->join('tbl_thanhvien', 'tbl_donhang.id_thanhvien=tbl_thanhvien.id');
$CI->db->where('tbl_donhang.id', $id);
$query = $CI->db->get();
$chitietkhachhang = $query->result();
$chitietkhachhang = $chitietkhachhang[0];
$temp = split(' ', $chitietkhachhang->ngaygui);
$temp1 = split('-', $temp[0]);
$nam = $temp1[0];
$thang = $temp1[1];
$ngay = $temp1[2];

$query = $CI->db->query('select * from tbl_chitietdonhang,tbl_sim where tbl_chitietdonhang.id_sim=tbl_sim.id and id_donhang='.$id);
$danhsachsim = $query->result();
$CI->load->model('main_model');
$danhsachmang = $CI->main_model->get_danhsachmang();
$mangdanhsachmang = array();
foreach($danhsachmang as $r)
{
	$mangdanhsachmang[$r->id] = $r;
}
?>

<div>
	<div>
    	ID thành viên: <?=$chitietkhachhang->id?>
    </div>
    <div>
    	Tài khoản: <?=$chitietkhachhang->taikhoan?>
    </div>
    <div>
    	Họ tên: <?=$chitietkhachhang->hoten?>
    </div>
    <div>
    	Số điện thoại: <?=$chitietkhachhang->sodienthoai?>
    </div>
    <div>
    	Địa chỉ: <?=$chitietkhachhang->diachi?>
    </div>
    <div>
    	Ngày gửi: <?= $ngay . '/' . $thang . '/' . $nam . ' ' . $temp[1] ?>
    </div>
    <div>
    	Ý kiến phản hồi: <?=$chitietkhachhang->ykien?>
    </div>
    <form method="post">
        <table border="1px" cellpadding="0" cellspacing="0" width="100%">
            <tr>
                <th>ID</th>
                <th>Số sim</th>
                <th>Giá</th>
                <th>Mạng</th>
                <th>Trạng thái</th>
                <th>Đặt cọc bởi</th>
                <th>Đặt cọc</th>
                <th>Bán</th>
            </tr>
    <!----------------------------------------------------------->
<?php
            foreach($danhsachsim as $sim)
            {
?>
                <tr>
                    <td><?=$sim->id?></td>
                    <td><?=$sim->sosim?></td>
                    <td><?=$sim->gia?></td>
                    <td><?=$mangdanhsachmang[$sim->mang]->tenmang?></td>
                    <td>
					<?php 
						if($sim->trangthai==1) 
							echo 'Đã bán'; 
						else if($sim->trangthai==0)
							echo 'Chưa bán';
						else if($sim->trangthai==2)
							echo 'Đặt cọc';
					?>
                    </td>
                    <td>
<?php
					if($sim->trangthai == 2)
					{
						$CI->db->where('id', $sim->id_thanhviendatcoc);
						$query = $CI->db->get('tbl_nguoidatcoc');
						$nguoidatcoc = $query->row();
						echo 'Họ tên: ' . $nguoidatcoc->hoten . '<br>';
						echo 'Điện thoại: ' . $nguoidatcoc->dienthoai . '<br>';
						echo 'Email: ' . $nguoidatcoc->email . '<br>';
						echo 'Địa chỉ: ' . $nguoidatcoc->diachi . '<br>';
						echo 'Ghi chú: ' . $nguoidatcoc->ghichu . '<br>';
						echo 'Số tiền đặt cọc: ' . $sim->sotiendatcoc . '.000<br>';
						echo 'Ngày đặt cọc: ';
						$temp1 = split(' ', $sim->ngaydatcoc);
						$temp2 = split('-', $temp1[0]);
						echo $temp2[2] . '/' . $temp2[1] . '/' . $temp2[0] . ' ' . $temp1[1];
					}
?>
                    </td>
                    <td>
<?php
						if($sim->trangthai == 0)
						{
?>
	                    	<a href="<?=site_url('admin/quanly/datcoc/'.$sim->id.'/'.$chitietkhachhang->id.'/'.$id)?>">Đặt cọc</a>
<?php
						}
						else if($sim->trangthai == 2)
						{
?>
							<a href="<?=site_url('admin/quanly/huydatcoc/'.$sim->id.'/'.$id)?>">Hủy đặt cọc</a>
<?php
						}
?>
                    </td>
                    <td><input type="text" name="giabanduoc/<?=$sim->id?>" size="7" value="<?php
						if($sim->trangthai == 1)
							echo $sim->giabanduoc;
						else
							echo $sim->gia;
					?>" /></td>
                    <td><input type="submit" name="ban/<?=$sim->id?>" value="Bán"/></td>
                </tr>
<?php
            }
?>
        </table>
        <br>
        <div align="center">
        	<input type="submit" name="duyetxong" value="Duyệt xong"/>
        </div>
    </form>
</div>